package com.example.mvvmdemo.viewmodel;

import android.app.Application;
import android.content.Context;
import androidx.lifecycle.MutableLiveData;
import com.example.common.base.BaseViewModel;
import com.example.common.utils.LocaleManager;
import com.example.common.utils.LogUtil;

public class LanguageViewModel extends BaseViewModel {
    
    public MutableLiveData<String> currentLanguageDisplay = new MutableLiveData<>();
    public MutableLiveData<LocaleManager.LanguageOption> selectedLanguage = new MutableLiveData<>();
    public MutableLiveData<Boolean> languageChanged = new MutableLiveData<>();
    
    private Context context;
    
    public LanguageViewModel() {
        super();
    }
    
    public void setContext(Context context) {
        this.context = context;
        initCurrentLanguage();
    }
    
    private void initCurrentLanguage() {
        if (context != null) {
            LocaleManager.LanguageOption currentOption = LocaleManager.getSavedLanguage(context);
            selectedLanguage.setValue(currentOption);
            currentLanguageDisplay.setValue(currentOption.displayName);
            
            LogUtil.d("LanguageViewModel", "Current language: " + currentOption.displayName);
        }
    }
    
    public void selectLanguage(LocaleManager.LanguageOption option) {
        LocaleManager.LanguageOption current = selectedLanguage.getValue();
        
        if (current == null || !current.language.equals(option.language)) {
            selectedLanguage.setValue(option);
            currentLanguageDisplay.setValue(option.displayName);
            
            // 保存语言设置
            if (context != null) {
                LocaleManager.setLocale(context, option);
                languageChanged.setValue(true);
            }
            
            LogUtil.d("LanguageViewModel", "Language changed to: " + option.displayName);
        }
    }
    
    public boolean isChineseSelected() {
        LocaleManager.LanguageOption current = selectedLanguage.getValue();
        return current != null && LocaleManager.LANGUAGE_CHINESE.equals(current.language);
    }
    
    public boolean isEnglishSelected() {
        LocaleManager.LanguageOption current = selectedLanguage.getValue();
        return current != null && LocaleManager.LANGUAGE_ENGLISH.equals(current.language);
    }
    
    public LocaleManager.LanguageOption getCurrentLanguage() {
        return selectedLanguage.getValue();
    }
}